#ifndef CHESS_H
#define CHESS_H

#include <fstream>
#include <cmath>
#include <algorithm>
#include <exception>

//GUI stuff
#include <CEGUI.h>
#include <RendererModules/Direct3D10/CEGUIDirect3D10Renderer.h>


//resource manager stuff
#include "ResourcesManager.h"

//message stuff
#include "d3dUtil.h"
#include "MessageQueryer.h"
#include "MessageDispatcher.h"
#include "GlobalMessage.h"

//tool stuff
#include "Logger.h"
#include "InitializeParameter.h"
#include "d3dapp.h"

//scene manager stuff
#include "SceneManager.h"
#include "SceneNode.h"
#include "Camera.h"
#include "Ray.h"

//boost stuff
#include <boost/thread/thread.hpp>
#include <boost/thread/mutex.hpp>

//opening screen
#include "OpeningScreen.h"

//chess stuff
#include "BoardVertexInfo.h"
#include "ChessBoard.h"
#include "ChessType.h"
#include "ChessPlayer.h"
#include "HumanPlayer.h"
#include "AiPlayer.h"

//message stuff
#include "Message.h"
#include "MouseListener.h"
#include "MouseSubject.h"
#include "KeyboardSubject.h"
#include "KeyboardListener.h"

class Chess : public KeyboardListener , public MouseListener
{
public:
	Chess(void);
	~Chess(void);

	void Init(ID3D10Device* pDevice , HINSTANCE mhAppInst);
	void UpdateScene(float dt);
	void Draw(void);
	void CalculateAI();

	virtual void KeyboardUpdate(KeyboadMsg msg);
	virtual void MouseUpdate(MouseMsg msg,const Point& oldPoint,const Point& currPoint);

private:
	void				LoadResource(void);
	void				SetCamera(void);

	//move a chess man from one node to another,and calculate the translation from it's parent
	void				MoveNode(const ChessManMoveInfo& info);
	inline std::string	GetNodeNameFromCoord(const BoardCoord& coord);
	inline std::string	GetChessManNameByType(int chessManType);
	void				DeleteChessManFromNode(const BoardCoord& coord,int chessManType);

	//give a point in the screen,return the selected coordinate
	//return (-1,-1) if nothing has been selected
	BoardCoord			Picking(const Point& mousePoint);

	//give a point in the screen return a ray in the view space
	inline Ray			GetRay(const Point& mousePoint);

	//pawn promotion ,make one pawn become a queen
	void				PawnPromotion(const ChessBoard& board,const BoardCoord& promotionCoord);

	//move the queen copy to the right position
	void				MoveQueen(SceneNode* pQueen,int chessmanType,const BoardCoord& dest);
	//make the queen's copy for pawn promotion, must be call at the chessboard's initialization
	void				MakeQueenCopy();

	//castle move rook node
	inline void			MoveCastleRook();

///////////////CEGUI/////////////////////////////////////////////////
	void				InitGUI();
	void				UpdateGUI();

private:
	ID3D10Device*			m_pDevice;

	InitializeParameter		m_InitializeParameter;
	SceneManager			m_SceneMgr;
	ChessPlayer*			m_pCurrPlayer;
	ChessPlayer*			m_pAnotherPlayer;

	SceneNode*				m_pWhiteQueenCopy;
	SceneNode*				m_pBlackQueenCopy;

//////////////////////////////////////////////////////////////////////////
	CEGUI::Window*			m_pRootWindow;
};


#endif
